<?php
declare (strict_types=1);

namespace app\admin\controller;


use app\common\builder\TableBuilder;
use app\common\builder\TableFront;
use app\common\model\Area;
use app\common\tool\Result;
use think\App;
use app\common\model\Business;

class OrderRefund extends Base
{

    //主键，默认为id
    protected $pk = 'id';

    //主模型
    protected $model = '\app\common\model\OrderRefund';

    //搜索条件
    protected $search;

    //关联模型
    protected $hasOne = ['user', 'express', 'business', 'father'];

    protected $append = ['status_text'];

    //页面所需表头参数，渲染相关参数参考layui官方文档，目前暂时为静态，后期考虑存库
    protected $table;


    public function index()
    {
        return $this->setTable();
    }

    public function setTable()
    {
        //构建table
        $tableFront = TableFront::getInstance()
            ->addRow(['type' => 'checkbox', 'fixed' => 'left', 'title' => '全选'])
            ->addRow(['field' => 'id', 'title' => 'ID', 'width' => 90])
            ->addRow(['field' => 'sn', 'title' => '退款编号'])
            ->addRow(['field' => 'order_sn', 'title' => '原订单编号', 'templet' => '<div>{{d.father?d.father.sn:""}}</div>'])
            ->addRow(['field' => 'business_title', 'title' => '所属商家', 'templet' => '<div>{{d.business?d.business.title:"【未支付】"}}</div>'])
            ->addRow(['field' => 'pay_way', 'title' => '退回方式'])
            ->addRow(['field' => 'product_status', 'title' => '商品状态', 'templet' => '<div>{{d.product_status==1?"未收到货":"已收到货"}}</div>'])
            ->addRow(['field' => 'type', 'title' => '退款类型', 'templet' => '<div>{{d.type==1?"退款":"退款退货"}}</div>'])
            ->addRow(['field' => 'reason', 'title' => '退款原因'])
            ->addRow(['field' => 'total', 'title' => '退款金额'])
            ->addRow(['field' => 'code', 'title' => '下单会员', 'templet' => '<div>{{d.user.code}}</div>'])
            ->addRow(['field' => 'create_time', 'title' => '申请时间'])
            ->addRow(['field' => 'status_text', 'title' => '退款状态'])
            ->addRow(['field' => 'refund_time', 'title' => '退款时间'])
            ->addRow(['field' => 'receiver_tel', 'title' => '收件人手机号'])
            ->addRow(['field' => 'receiver', 'title' => '收件人'])
            ->addRow(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#barDemo', 'title' => '操作']);

        return TableBuilder::getInstance()
            ->addColumns($tableFront)
            ->setSearch($this->setSearch())
            ->addJsUrl(['order'])
            ->fetch('order_refund/index');
    }

    public function setSearch()
    {
        return TableFront::getInstance()
            ->setSearch(['uid', '会员编号/昵称/手机号', 'input', '', '\app\common\model\User', 'code|nickname|mobile,id'])
            ->setSearch(['business_id', '商家名称', 'input', '', '\app\common\model\Business', 'title,id'])
            ->setSearch(['sn', '退款编号', 'input'])
            ->setSearch(['status', '退款状态', 'select', getOrderRefundStatus('', 2)]);
    }

    public function edit($name = '', $id = '', $tpl = 'order_refund/edit', $info = '')
    {
        $info = $this->model::find($id);

        return parent::edit($name, $id, $tpl, $info); // TODO: Change the autogenerated stub
    }

    public function examine()
    {
        try {
            $param = $this->request->post();
            $business_id = $this->model::where('id', $param['id'])->value('business_id');
            $business = Business::find($business_id);
            $param['type'] = 'examine';
            return Result::fastReturn($this->model::businessEdit($param, $business));
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }

    public function receipt()
    {
        try {
            $param = $this->request->post();
            $business_id = $this->model::where('id', $param['id'])->value('business_id');
            $business = Business::find($business_id);
            $param['type'] = 'receipt';
            return Result::fastReturn($this->model::businessEdit($param, $business));
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
}
